package ywh.ui
{
	import fl.controls.listClasses.ICellRenderer;
	import fl.controls.TileList;
	import fl.core.InvalidationType;
	import fl.events.InteractionInputType;
	import fl.events.ListEvent;
	import fl.events.SliderEvent;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	/**
	 * ...
	 * @author 杨伟华
	 */
	public class PhotoTile extends TileList
	{
		protected var _hslider:Sprite;
		private var _mouseDownX:Number;
		private var _mouseDownContentX:Number;
		private var downTime:Number;
		private var _clickTime:Number=100;
		private static var defaultStyles:Object = {
				cellRenderer:MyPhotoCell,
				focusRectSkin:null,
				focusRectPadding:null,
				skin:"TileList_skin"
			};        
		public static function getStyleDefinition():Object
		{
			return mergeStyles(defaultStyles, TileList.getStyleDefinition());
		}
		
		public function PhotoTile()
		{
			super();
			addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
		}
		
		override protected function configUI():void
		{
			super.configUI();
			removeChild(_horizontalScrollBar);
			removeChild(_verticalScrollBar);
			_horizontalScrollBar = new NoScrollBar();
			_verticalScrollBar = new NoScrollBar();
			var slider:SliderForContent = new SliderForContent();
			slider.maximum = 1000;
			slider.minimum = 0;
			slider.snapInterval = 0.1;
			slider.addEventListener(MouseEvent.MOUSE_DOWN, handleSliderMouseDown);
			slider.addEventListener(SliderEvent.CHANGE, handleSliderChange);
			_hslider = slider;
			addChild(_hslider);
		}

		override public function set columnCount(value:uint):void 
		{
			super.columnCount = 1;
		}
		override protected function draw():void
		{
			if (isInvalid(InvalidationType.SIZE))
			{
				//__columnCount = 1;
				rowHeight = height-15;
				_hslider.x = 50;
				_hslider.y = rowHeight+5;
				_hslider.width = width - 100;
			}
			super.draw();
		}
		
		private function handleSliderChange(e:SliderEvent):void
		{
			if ((e as SliderEvent).triggerEvent == InteractionInputType.MOUSE)
			{
				var slider:SliderForContent = _hslider as SliderForContent;
				horizontalScrollPosition = slider.value / slider.maximum * maxHorizontalScrollPosition;
			}
		}
		private function handleSliderMouseDown(e:MouseEvent):void
		{
			e.stopPropagation();
		}
		
		private function handleMouseDown(e:MouseEvent):void
		{
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, handleStageMouseMove);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, handleStageMouseUp);
			_mouseDownX = e.stageX;
			_mouseDownContentX = this.horizontalScrollPosition;
		}
		
		private function handleStageMouseMove(e:MouseEvent):void
		{
			var targetX:Number =  _mouseDownX-e.stageX + _mouseDownContentX;
			
			if (targetX < 0)
			{
				horizontalScrollPosition = 0;
			}
			else if (targetX > maxHorizontalScrollPosition)
			{
				horizontalScrollPosition = maxHorizontalScrollPosition;
			}
			else
			{
				horizontalScrollPosition = targetX;
			}
		}
		
		private function handleStageMouseUp(e:MouseEvent):void
		{
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleStageMouseMove);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, handleStageMouseUp);
		}
		
		private function handleRendererSpriteMouseDown(e:MouseEvent):void
		{
			downTime = getTimer();
		}
		
		private function handleRendererSpriteMouseUp(event:MouseEvent):void
		{
			var upTime:Number = getTimer();
			if ((upTime-downTime) < _clickTime) {
				var renderer:ICellRenderer = event.currentTarget as ICellRenderer;
				var itemIndex:uint = renderer.listData.index;
				dispatchEvent(new ListEvent(ListEvent.ITEM_CLICK, false, true, renderer.listData.column, renderer.listData.row, itemIndex, renderer.data));
			}
		}
		
		override protected function setHorizontalScrollPosition(scroll:Number, fireEvent:Boolean = false):void
		{
			super.setHorizontalScrollPosition(scroll, fireEvent);
			var slider:SliderForContent = _hslider as SliderForContent;
			slider.value = slider.maximum * horizontalScrollBar.scrollPosition / horizontalScrollBar.maxScrollPosition;
		}
		override protected function drawList():void
		{
			super.drawList();
			for each (var renderer:Object in activeCellRenderers) {
				var rendererSprite:Sprite = renderer as Sprite;
				if (rendererSprite != null) {
					rendererSprite.removeEventListener(MouseEvent.CLICK, handleCellRendererClick);
					rendererSprite.removeEventListener(MouseEvent.ROLL_OVER, handleCellRendererMouseEvent);
					rendererSprite.removeEventListener(MouseEvent.ROLL_OUT, handleCellRendererMouseEvent);
					rendererSprite.removeEventListener(MouseEvent.DOUBLE_CLICK, handleCellRendererDoubleClick);
					rendererSprite.addEventListener(MouseEvent.MOUSE_DOWN, handleRendererSpriteMouseDown);
					rendererSprite.addEventListener(MouseEvent.MOUSE_UP, handleRendererSpriteMouseUp);
				}
			}
		}
		
		public function set clickTime(value:Number):void
		{
			_clickTime = value;
		}
	}
}